package com.avertymaroc.app;

import static com.avertymaroc.library.C.TAG_AUTH_SUCCES;
import static com.avertymaroc.library.C.TAG_FINISH;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.avertymaroc.library.SessionManager;
import com.avertymaroc.users.Authentification;
import com.avertymaroc.users.HomeActivity;

public class MainActivity extends Activity {

	public static int THEME = R.style.Theme_Sherlock_NoActionBar;
	private final int RC_AUTH = 1000;
	private final int RC_HOME = 1001;
	private SessionManager session = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
//		Intent i = new Intent(this ,TestActivity.class );
//		startActivity(i);

	}

	@Override
	protected void onResume() {
		super.onResume();
		session = new SessionManager(MainActivity.this);
		if (session.isLoggedIn()) {
			
			Intent intent = new Intent(MainActivity.this, HomeActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			
			// Add new Flag to start new Activity
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivityForResult(intent, RC_HOME);
		} else {
			Intent i = new Intent(MainActivity.this, Authentification.class);
			startActivityForResult(i, RC_AUTH);
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == TAG_FINISH) {
		finish();
		}
		Intent intent;
		switch (requestCode) {
		case RC_HOME:
			
			intent = new Intent(getApplicationContext(), Authentification.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			
			// Add new Flag to start new Activity
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			
			startActivity(intent);
			break;
		case RC_AUTH:
			 if (resultCode == TAG_AUTH_SUCCES){
				intent = new Intent(getApplicationContext(), HomeActivity.class);
				startActivity(intent);
			}
			break;
		}

	}

}